import Vue from 'vue'
import Router from 'vue-router'
import {
  getUserType
} from '@/utils/auth'

Vue.use(Router)

const userType = getUserType()
const userPermBlackList = ['0', '2']
console.log('userType================', userType);


import Layout from '@/views/layout/Layout'

export const constantRoutes = [{
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [{
      path: '/redirect/:path(.*)',
      component: () => import('@/views/redirect/index')
    }]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/authredirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/errorPage/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/errorPage/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard',
    children: [{
      path: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      name: 'Dashboard',
      meta: {
        title: '首页',
        icon: 'dashboard',
        affix: true
      }
    }]
  },
  {
    path: '/user',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    hidden: userType == '1',
    name: 'userManage',
    meta: {
      title: '用户管理',
      icon: 'chart'
    },
    children: [{
      path: 'user',
      component: () => import('@/views/user/user'),
      name: 'user',
      //店铺不可见
      hidden: userType == '1',
      meta: {
        title: '用户管理',
        noCache: true
      }
    }, {
      path: 'shopOwner',
      component: () => import('@/views/user/shopOwner'),
      name: 'user',
      //店铺不可见
      hidden: userType == '1',
      meta: {
        title: '店长管理',
        noCache: true
      }
    }, ]
  },

  {
    path: '/mall',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'mallManage',
    meta: {
      title: '商场管理',
      icon: 'chart'
    },
    children: [

      {
        path: 'category',
        component: () => import('@/views/mall/category'),
        name: 'category',
        // 管理员不可见
        hidden: userType == '0' || userType == '2',
        meta: {
          title: '商品类目',
          noCache: true
        }
      },
      {
        path: 'order',
        component: () => import('@/views/mall/order'),
        name: 'order',
        meta: {
          perms: ['GET /admin/order/list', 'GET /admin/order/detail', 'POST /admin/order/ship', 'POST /admin/order/refund', 'POST /admin/order/delete', 'POST /admin/order/reply'],
          title: '订单管理',
          noCache: true
        }
      },
      {
        path: 'shopInfo',
        component: () => import('@/views/mall/shopInfo'),
        name: 'shopInfo',
        hidden: userType != '0',
        meta: {
          title: '店铺管理',
          noCache: true
        }
      },
      {
        path: 'shopDetailInfo',
        component: () => import('@/views/mall/shopDetailInfo'),
        name: 'shopDetailInfo',
        hidden: userType === '0',
        meta: {
          title: '店铺维护',
          noCache: true
        }
      },
    ]
  },

  {
    path: '/goods',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'goodsManage',
    // 管理员不可见
    hidden: userType == '0' || userType == '2',
    meta: {
      title: '商品管理',
      icon: 'chart'
    },
    children: [{
        path: 'list',
        component: () => import('@/views/goods/list'),
        name: 'goodsList',
        // 管理员不可见
        hidden: userType == '0' || userType == '2',
        meta: {
          perms: ['GET /admin/goods/list', 'POST /admin/goods/delete'],
          title: '商品列表',
          noCache: true
        }
      },
      {
        path: 'create',
        component: () => import('@/views/goods/create'),
        name: 'goodsCreate',
        // 管理员不可见
        hidden: true,
        meta: {
          perms: ['POST /admin/goods/create'],
          title: '商品上架',
          noCache: true
        }
      },
      {
        path: 'edit',
        component: () => import('@/views/goods/edit'),
        name: 'goodsEdit',
        // 管理员不可见
        hidden: true,
        meta: {
          perms: ['GET /admin/goods/detail', 'POST /admin/goods/update', 'POST /admin/goods/catAndBrand'],
          title: '商品编辑',
          noCache: true
        },
      },
    ]
  },
  {
    path: '/stat',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    name: 'statManage',
    hidden: true,
    meta: {
      title: '统计报表',
      icon: 'chart'
    },
    children: [{
        path: 'user',
        component: () => import('@/views/stat/user'),
        name: 'statUser',
        meta: {
          perms: ['GET /admin/stat/user'],
          title: '用户统计',
          noCache: true
        }
      },
      {
        path: 'order',
        component: () => import('@/views/stat/order'),
        name: 'statOrder',
        meta: {
          perms: ['GET /admin/stat/order'],
          title: '订单统计',
          noCache: true
        }
      },
      {
        path: 'goods',
        component: () => import('@/views/stat/goods'),
        name: 'statGoods',
        meta: {
          perms: ['GET /admin/stat/goods'],
          title: '商品统计',
          noCache: true
        }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: 'noredirect',
    alwaysShow: true,
    children: [{
        path: 'password',
        component: () => import('@/views/profile/password'),
        name: 'password',
        meta: {
          title: '修改密码',
          noCache: true
        }
      }     
    ],
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
